/*
 * TemplateView.java
 *
 * Created on 13. Juni 2008, 12:40
 */

package twi.query.template2.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.ArrayList;

import twi.query.template.NameFactory;

/**
 *
 * @author  Soeren
 */
public class TemplateView extends javax.swing.JPanel {

    private VariableChangeListener vcl;
	private ClassChooseListener ccl;
	private AddIndividuumListener ail;
	private MaximizeListener ml;
	/** Creates new form TemplateView 
	 * @param edit 
	 * @param string 
	 * @param arrayList */
    public TemplateView(boolean edit, ArrayList<String> arrayList, String string, ClassChooseListener ccl, VariableChangeListener vcl, AddIndividuumListener ail, MaximizeListener ml) {
    	this.ail = ail;
    	this.ml = ml;
    	this.vcl = vcl;
    	initComponents();
    	for (int i = 0; i < arrayList.size(); i++)
    		this.jComboBox1.addItem(arrayList.get(i));
    	this.jComboBox1.setSelectedItem(string);
    	this.jPanel1.setVisible(edit);
    	this.ccl = ccl;        
    	
    	this.jComboBox1.setEnabled(edit);
    	this.jCheckBox_vis_var.setSelected(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPanel1 = new javax.swing.JPanel();
        jCheckBox_vis_node = new javax.swing.JCheckBox();
        jCheckBox_vis_var = new javax.swing.JCheckBox();
        jCheckBox_opt = new javax.swing.JCheckBox();
        jButton_indi = new javax.swing.JButton();
        hide_panel = new javax.swing.JPanel();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setName("Form"); // NOI18N
        addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                formMouseClicked(evt);
            }
        });

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twi_project.TWI_ProjectApp.class).getContext().getResourceMap(TemplateView.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jComboBox1.setName("jComboBox1"); // NOI18N
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N
        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });
        jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jTextField1KeyTyped(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("jPanel1.border.title"))); // NOI18N
        jPanel1.setName("jPanel1"); // NOI18N

        jCheckBox_vis_node.setText(resourceMap.getString("jCheckBox_vis_node.text")); // NOI18N
        jCheckBox_vis_node.setEnabled(false);
        jCheckBox_vis_node.setName("jCheckBox_vis_node"); // NOI18N
        jCheckBox_vis_node.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_vis_nodeActionPerformed(evt);
            }
        });

        jCheckBox_vis_var.setText(resourceMap.getString("jCheckBox_vis_var.text")); // NOI18N
        jCheckBox_vis_var.setName("jCheckBox_vis_var"); // NOI18N
        jCheckBox_vis_var.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_vis_varActionPerformed(evt);
            }
        });

        jCheckBox_opt.setText(resourceMap.getString("jCheckBox_opt.text")); // NOI18N
        jCheckBox_opt.setName("jCheckBox_opt"); // NOI18N

        jButton_indi.setText(resourceMap.getString("jButton_indi.text")); // NOI18N
        jButton_indi.setName("jButton_indi"); // NOI18N
        jButton_indi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_indiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jCheckBox_opt)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox_vis_node)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCheckBox_vis_var))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jButton_indi, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(10, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jCheckBox_opt)
                    .addComponent(jCheckBox_vis_node)
                    .addComponent(jCheckBox_vis_var))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton_indi)
                .addContainerGap())
        );

        hide_panel.setName("hide_panel"); // NOI18N

        javax.swing.GroupLayout hide_panelLayout = new javax.swing.GroupLayout(hide_panel);
        hide_panel.setLayout(hide_panelLayout);
        hide_panelLayout.setHorizontalGroup(
            hide_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 261, Short.MAX_VALUE)
        );
        hide_panelLayout.setVerticalGroup(
            hide_panelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 156, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jComboBox1, 0, 179, Short.MAX_VALUE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE)))
            .addComponent(hide_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(hide_panel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed

}//GEN-LAST:event_jTextField1ActionPerformed

private void jCheckBox_vis_nodeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_vis_nodeActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox_vis_nodeActionPerformed

private void jCheckBox_vis_varActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_vis_varActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jCheckBox_vis_varActionPerformed

private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
	if (ccl != null)
		this.ccl.chooseClass((String) this.jComboBox1.getSelectedItem());
	String n = this.vcl.setVariableName(NameFactory.createNameFromClassName((String)this.jComboBox1.getSelectedItem()), true);
	this.jTextField1.setText(n);
//	this.jTextField1.getText(this.vcl.setVariableName(NameFactory.createNameFromClassName((String) this.jComboBox1.getSelectedItem())), true);
	
}//GEN-LAST:event_jComboBox1ItemStateChanged

private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField1KeyTyped
    String n = this.jTextField1.getText();
    String nn = this.vcl.setVariableName(n, false);
    if (!nn.equals(n)){
        this.jTextField1.setForeground(Color.RED);
    }else
        this.jTextField1.setForeground(Color.BLACK);
}//GEN-LAST:event_jTextField1KeyTyped

private void jButton_indiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_indiActionPerformed
	this.ail.addIndividum();
}//GEN-LAST:event_jButton_indiActionPerformed

private void formMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_formMouseClicked
	if (this.ml != null)
		this.ml.rekMaximize();
}//GEN-LAST:event_formMouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel hide_panel;
    private javax.swing.JButton jButton_indi;
    private javax.swing.JCheckBox jCheckBox_opt;
    private javax.swing.JCheckBox jCheckBox_vis_node;
    private javax.swing.JCheckBox jCheckBox_vis_var;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
	private TemplatePropertyView propertyPanel;
    
    
	public void addPropertyWindow(TemplatePropertyView templatePropertyView) {
		this.hide_panel.removeAll();
		this.hide_panel.setLayout(new BorderLayout());
		this.hide_panel.add(templatePropertyView, BorderLayout.CENTER);
	}

	
	public void setHidePanel(TemplatePropertyView templatePropertyView) {
		this.propertyPanel=templatePropertyView;
		this.hide_panel.removeAll();
		this.hide_panel.setLayout(new BorderLayout());
		this.hide_panel.add(templatePropertyView, BorderLayout.CENTER);
		templatePropertyView.setVisible(true);
		this.hide_panel.setVisible(true);
		validate();
	}

	public void setMaximized(boolean b) {
		this.hide_panel.setVisible(b);
	}

	public boolean isOptional() {
		return this.jCheckBox_opt.isSelected();
	}

	public boolean showVariable() {
		return this.jCheckBox_vis_var.isSelected();
	}

	public String getPropertyValue(String prop) {
		if (this.propertyPanel != null)
			return this.propertyPanel.getPropertyValue(prop);
		return null;
	}

	

}
